package com.ica.android.ui.command;

import com.ica.R;
import com.ica.model.facade.CommandFacadeMockImpl;
import com.ica.model.command.AbstractCommand;
import com.ica.model.command.AcceptCallCommand;
import com.ica.model.command.RejectCallCommand;
import com.ica.model.command.RingingModeCommand;
import com.ica.model.command.SendSmsCommand;
import com.ica.model.command.SilenceModeCommand;
import com.ica.model.command.VibrateModeCommand;

import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.ToggleButton;

/**
 * @author   Hector
 */
public abstract class CommandActivity extends Activity {
	
	/**
	 * @uml.property  name="command"
	 * @uml.associationEnd  
	 */
	protected AbstractCommand command = null;
	/**
	 */
	protected EditText etName = null;
	/**
	 * @uml.property  name="commandFacade"
	 * @uml.associationEnd  
	 */
	protected final CommandFacadeMockImpl commandFacade = CommandFacadeMockImpl.getInstance();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
	}
	
	protected void init(int layout) {	
		setContentView(layout);
        Button save = (Button) findViewById(R.id.bSaveCommand);
        etName = (EditText) findViewById(R.id.etCommandName);
        save.setOnClickListener(onClickSave());
        final ToggleButton acceptCall = (ToggleButton) findViewById(R.id.bAcceptCall);
		final ToggleButton rejectCall = (ToggleButton) findViewById(R.id.bRejectCall);
        acceptCall.setOnCheckedChangeListener(new OnCheckedChangeListener() {	
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked)
					rejectCall.setChecked(false);
			}
		});
        rejectCall.setOnCheckedChangeListener(new OnCheckedChangeListener() {	
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked)
					acceptCall.setChecked(false);
			}
		});
	}
	
	protected void getCommandDefined(AbstractCommand command) {
		
		AbstractCommand c = command;
		ToggleButton acceptCall = (ToggleButton) findViewById(R.id.bAcceptCall);
		ToggleButton rejectCall = (ToggleButton) findViewById(R.id.bRejectCall);
		ToggleButton sendSms = (ToggleButton) findViewById(R.id.bSendSms);
		RadioButton ringingMode = (RadioButton) findViewById(R.id.rRingMode);
		RadioButton silentMode = (RadioButton) findViewById(R.id.rSilentMode);
		RadioButton vibrateMode = (RadioButton) findViewById(R.id.rVibrateMode);
		c.getCommands().clear();
		if (acceptCall.isChecked())
			c.addCommand(new AcceptCallCommand());
		if (rejectCall.isChecked())
			c.addCommand(new RejectCallCommand());
		if (sendSms.isChecked())
			c.addCommand(new SendSmsCommand());
		if (ringingMode.isChecked())
			c.addCommand(new RingingModeCommand());
		if (silentMode.isChecked())
			c.addCommand(new SilenceModeCommand());
		if (vibrateMode.isChecked())
			c.addCommand(new VibrateModeCommand());
	}

	protected abstract OnClickListener onClickSave();

}
